<?php

namespace App\Services\Draw\DrawObject;


use App\Jobs\QiPao\Draw\FantasyDrawStatisticsJob;
use App\Models\MainDB\Gift;
use App\Services\Draw\Enum\DrawEnum;
use App\Services\Draw\PoolGroupObject\FantasyPublicPoolGroup;
use App\Services\Draw\PoolGroupObject\FantasyIndependentPoolGroup;
use App\Services\Draw\PoolGroupObject\FantasyNovicePoolGroup;
use App\Services\Draw\PoolGroupObject\PublicPoolGroup;
use App\Services\Draw\PoolGroupObject\FantasyQueuePoolGroup;
use App\Services\Tools\PushChatService;
use App\Services\Tools\PushService;
use App\Services\Tools\RedisService;
use Illuminate\Support\Facades\DB;
use App\Repositories\GiftRepository;
use App\Enums\RedisKeyEnum;

/**
 * 许愿魔法书抽奖实体
 * @author wrh
 */
class Fantasy extends Draw
{

    protected $isDrawCriticalGift = true;
    protected $cachePrefix = 'fantasy:';
    protected $privateKey = 'fantasy';
    protected $balanceNotEnoughMsg = '魔法书不足，请前往购买';
    protected $isNovicePreset = true; //是否新手奖池预设

    public function __construct()
    {
        parent::__construct();
        $this->redisObject = app('redis')->connection('game');
        $this->poolConfigModel = app('model_fantasy_pool_config');
        $this->poolUserModel = app('model_fantasy_pool_user');
        $this->poolModel = app('model_fantasy_pool');
        $this->poolPrizeModel = app('model_fantasy_pool_prize');
        $this->poolGroupModel = app('model_fantasy_pool_group');
        $this->poolQueueModel = app('model_fantasy_pool_queue');
        $this->giftTemplateModel = app('model_fantasy_gift_template');
        $this->poolTemplateModel = app('model_fantasy_pool_template');
        $this->poolDrawModel = app('model_fantasy_pool_draw');
        $this->poolDrawRecordModel = app('model_fantasy_pool_draw_record');
        $this->levelModel = app('model_fantasy_level');
        $this->criticalGiftModel = app('model_fantasy_critical_gift');
        $this->poolIndependentModel = app('model_fantasy_pool_independent');
        $this->poolIndependentPrizeModel = app('model_fantasy_pool_independent_prize');
        $this->criticalDrawRecordModel = app('model_fantasy_critical_draw_record');

        $this->appointGiftModel = app('model_fantasy_appoint_gift');

        $this->checkRecordModel = app('model_fantasy_draw_check_record');
        $this->addPoolAccountRecordModel = app('model_fantasy_draw_add_pool_account_record');
        $this->addPoolRepo = app('repo_add_pool_fantasy');

        $this->queuePoolGroup = new FantasyQueuePoolGroup($this->redisObject);
        $this->independentPoolGroup = new FantasyIndependentPoolGroup($this->redisObject);
        $this->publicPoolGroup = new FantasyPublicPoolGroup($this->redisObject);
        $this->novicePoolGroup = new FantasyNovicePoolGroup($this->redisObject);
    }

    public function push($user, array $notifyPushData, array $broadcastPushData, array $extraPushData)
    {
        try {
            $userChatLabel = RedisService::getUserChatLabel($user->user_id);
            $levelInfo = $this->levelModel->one(['level' => $user->level]);
            $uni_key = uniqidReal();
            $getMillisecond = getMillisecond();
            $PushService = new PushService();
            // /幸运票品
            $ext_data = $userChatLabel;
            $extra_data = $userChatLabel;
            $notifyPush = [];
            if (!empty($notifyPushData)) {
                $txt = "<font color='#BDF1FF'>" . $levelInfo->name . " </font>";
                $txt .= "<font color='#3AA4E6'>" . $userChatLabel['nickname'] . " </font>";
                $txt .= "<font color='#BDFFD1'>在许愿魔法书中意外获得 </font>";
                foreach ($notifyPushData as $key => $value) {
                    $txt .= '<font color="#FFE6BC">' . $value->price . '</font>';
                    $txt .= '<font color="#BDFFD1"> 金币的</font>';
                    $txt .= '<font color="#3ADBE6">' . $value->name . ' x ' . $value->count . '.' . '</font>';
                }
                $txt .= '<font color="#BDFFD1"> 礼物，运气爆棚！</font>';
                $ext_data['type'] = PushService::GAME_PUSH_STATUS;
                $ext_data['action'] = 1;
                $ext_data['show_area'] = [
                    PushChatService::AREA_LUCKY,
                    PushChatService::AREA_ALL,
                ];
                $ext_data['show_label_id'] = [
                    PushChatService::AREA_LUCKY,
                ];
                $ext_data['bg_type'] = $this->privateKey;
                $notifyPush = [
                    'content' => $txt,
                    'uni_key' => $uni_key,
                    'extra' => $ext_data
                ];
            }
            $broadcastPush = [];
            if (!empty($broadcastPushData)) {
                $txt = "<font color='#FFFFFF'>" . $levelInfo->name . " </font><font color='#FFEB66'>" . $userChatLabel['nickname'] . "</font><font color='#FFFFFF'> 在许愿魔法书中意外获得 </font>";
                $txt_extra = [];
                foreach ($broadcastPushData as $gift) {
                    $txt_extra [] = [
                        'text' => $txt . "<font color='#FFEB66'>" . $gift->price . "金币的</font>" . "<font color='#73E8FF'>" . $gift->name . "</font><font color='#FFFFFF'>X" . $gift->count . "</font>",
                        'picture' => $gift->picture,
                    ];

                }
                $txt_extra[count($txt_extra) - 1]['text'] .= "<font color='#FFFFFF'>，震摄八方！</font>";
                $broadcastPush = [
                    'uni_key' => $uni_key,
                    'time' => $getMillisecond,
                    'msg' => $txt_extra,
                    'special' => RedisService::getBroadcastSpecial($user->user_id),
                    'bg_type' => $this->privateKey,
                    'show_grade' => $this->showBroadcastGrade(),
                ];
            }

            $extraPush = [];
            if (!empty($extraPushData)) {
                $extra_txt = "<font color='#FFD769'>" . $levelInfo->name . " </font>";
                $extra_txt .= "<font color='#E68D3A'>" . $userChatLabel['nickname'] . " </font>";
                $extra_txt .= "<font color='#46E0FF'>在许愿中获得 </font>";
                foreach ($extraPushData as $key => $value) {
                    $extra_txt .= '<font color="#FFEB6A">' . $gift['price'] . '</font>';
                    $extra_txt .= '<font color="#46E0FF"> 金币的等级专属礼物 </font>';
                    $extra_txt .= '<font color="#E6573A">' . $gift['name'] . '.' . '</font>';
                }
                $extra_txt .= '<font color="#46E0FF"> ，吉人天相！</font>';
                $extra_data['type'] = PushService::GAME_PUSH_STATUS;
                $extra_data['action'] = 1;
                $extra_data['show_area'] = [
                    PushChatService::AREA_LUCKY,
                    PushChatService::AREA_ALL,
                ];
                $extra_data['show_label_id'] = [
                    PushChatService::AREA_LUCKY,
                ];
                $extraPush = [
                    'content' => $extra_txt,
                    'uni_key' => $uni_key,
                    'extra' => $extra_data
                ];
            }

            $PushService->publicGamePush($notifyPush, $broadcastPush, $extraPush, [], $uni_key, $getMillisecond);

        } catch (\Exception $e) {
            return false;
        }
        return true;
    }

    public function getDrawJob($poolGroup, $user, $jobData)
    {
        return new FantasyDrawStatisticsJob($poolGroup, $user, $jobData, $this->cachePrefix);
    }


}
